package com.mindcoord.handbook.controller;

import com.alibaba.fastjson.JSON;
import com.mindcoord.handbook.common.Result;
import com.mindcoord.handbook.pojo.Major;
import com.mindcoord.handbook.service.MajorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping(value = "/major", produces = "application/json; charset=utf-8")
public class MajorController {

    private MajorService majorService;

    @Autowired
    public void setMajorService(MajorService majorService) {
        this.majorService = majorService;
    }

    @GetMapping("/")
    public Object listMajors() {
        Result result = majorService.listMajors();
        return JSON.toJSONString(result);
    }

    @GetMapping("/{id}")
    public Object getMajorById(@PathVariable("id") Integer id) {
        Result result = majorService.getMajorById(id);
        return JSON.toJSONString(result);
    }

    @PostMapping("/put")
    public Object insertMajor(Major major) {
        Result result = majorService.insertMajor(major);
        return JSON.toJSONString(result);
    }

    @DeleteMapping("/{id}")
    public Object deleteMajorById(@PathVariable("id") Integer id) {
        Result result = majorService.deleteMajorById(id);
        return JSON.toJSONString(result);
    }

    @PutMapping("/put")
    public Object updateMajor(Major major) {
        Result result = majorService.updateMajor(major);
        return JSON.toJSONString(result);
    }

}
